//
//  TopMenuViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-25.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "TopMenuViewController.h"
#import "Language.h"
#import "AppDelegate.h"

@implementation TopMenuViewController
@synthesize menuList;
@synthesize topMenus;
@synthesize delegate;
@synthesize chinaButton;
@synthesize taiwangButton;
@synthesize englishButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        selectRowId = -1;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    self.delegate = nil;
    [chinaButton release];
    [taiwangButton release];
    [englishButton release];
    [topMenus release];
    [menuList release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [menuList setDataSource:self];
    [menuList setDelegate:self];
    
	
    [chinaButton setImage:[UIImage imageNamed:@"Language_CN_Def.png"] forState:UIControlStateNormal];
    [taiwangButton setImage:[UIImage imageNamed:@"Language_GB_Def.png"] forState:UIControlStateNormal];
    [englishButton setImage:[UIImage imageNamed:@"Language_EN_Def.png"] forState:UIControlStateNormal];
    
    NSString *language = [AppDelegate getLanguage];
    if ([@"ft" isEqualToString:language]) {
        [taiwangButton setImage:[UIImage imageNamed:@"Language_GB_Se.png"] forState:UIControlStateNormal];
    } else if ([@"en" isEqualToString:language]) {
        [englishButton setImage:[UIImage imageNamed:@"Language_EN_Se.png"] forState:UIControlStateNormal];
    } else {
        [chinaButton setImage:[UIImage imageNamed:@"Language_CN_Se.png"] forState:UIControlStateNormal];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - close top menu

- (void)hiddenMenuAnimation:(UIView*)view {
    //view.frame = CGRectMake(0, -422, 320, 422);
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationDelay:0.0];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -460);
    view.transform = transform;
	[UIView commitAnimations];
}

- (IBAction)closeTopMenu:(id)sender {
    [self hiddenMenuAnimation:self.view];
}

- (IBAction)changeLanguage:(id)sender {
    Language *language = (Language *)[Language findFirstByCriteria:@"WHERE row_id=1"];
	if (!language) {
		language = [[[Language alloc] init] autorelease];
		language.rowId = 1;
	}
	
	UIButton *button = sender;
	if (button.tag == 100) {
		[chinaButton setImage:[UIImage imageNamed:@"Language_CN_Se.png"] forState:UIControlStateNormal];
		[taiwangButton setImage:[UIImage imageNamed:@"Language_GB_Def.png"] forState:UIControlStateNormal];
		[englishButton setImage:[UIImage imageNamed:@"Language_EN_Def.png"] forState:UIControlStateNormal];
        language.selectLanguage = @"jt";
	}
	if (button.tag == 101) {
		[chinaButton setImage:[UIImage imageNamed:@"Language_CN_Def.png"] forState:UIControlStateNormal];
		[taiwangButton setImage:[UIImage imageNamed:@"Language_GB_Se.png"] forState:UIControlStateNormal];
		[englishButton setImage:[UIImage imageNamed:@"Language_EN_Def.png"] forState:UIControlStateNormal];
        language.selectLanguage = @"ft";
	}
	if (button.tag == 102) {
		[chinaButton setImage:[UIImage imageNamed:@"Language_CN_Def.png"] forState:UIControlStateNormal];
		[taiwangButton setImage:[UIImage imageNamed:@"Language_GB_Def.png"] forState:UIControlStateNormal];
		[englishButton setImage:[UIImage imageNamed:@"Language_EN_Se.png"] forState:UIControlStateNormal];
		language.selectLanguage = @"en";
	}
	[language save];
    //[self closeTopMenu:nil];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate reloadApp];
}

#pragma mark -
#pragma mark Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [topMenus count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"NewsCellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font=[UIFont systemFontOfSize:12];
        cell.textLabel.textColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    // Configure the cell...
	NSUInteger row = [indexPath row];
	NSString *rowData = [topMenus objectAtIndex:row];
	
	cell.textLabel.text = rowData;
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger rowId = [indexPath row];
    if (selectRowId == rowId) {
        [self closeTopMenu:nil];
        return;
    }
    selectRowId = rowId;
    //NSLog(@"row=%d", rowId);
    if (delegate) {
        NSString *title = [topMenus objectAtIndex:rowId];
        [delegate changeViewController:rowId andTitle:title];
    }
    [self closeTopMenu:nil];
}

@end
